﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DNA.OpenAPI.Sitemap;
using DNA.Mvc.Services;
using System.IO;

namespace DNA.Mvc.Syndication
{
    public class WebPageSiteMapGenerator : SiteMapGeneratorBase
    {
        private WebSiteContext context;
        public WebPageSiteMapGenerator(WebSiteContext webContext) { context = webContext; }

        protected override void OnGenerateSiteMap(List<Sitemap> sitemaps)
        {
            var webs = context.DataContext.Webs.Filter(w => w.Name.Equals("home") || w.Type.Equals((int)WebTypes.Team) || w.Type.Equals((int)WebTypes.Public));
            foreach (var web in webs)
            {
                var _pageSitemap = CreateSiteMap(web);
                sitemaps.Add(_pageSitemap);
            }
        }

        private Sitemap CreateSiteMap(Web web)
        {
            var _pageSitemap = new Sitemap();
            string sitemapPath = HttpContext.Current.Server.MapPath("~/shared/public/sitemaps/");
            _pageSitemap.Location = context.ApplicationPath + "/webshared/home/sitemaps/" + web.Name + ".xml";
            var _recreatePageSitemap = false;

            if (!Directory.Exists(sitemapPath))
                Directory.CreateDirectory(sitemapPath);

            if (System.IO.File.Exists(sitemapPath + web.Name + ".xml"))
            {
                var fileInfo = new FileInfo(sitemapPath + web.Name + ".xml");
                if ((DateTime.Now - fileInfo.CreationTime).Days == 0)
                    _pageSitemap.LastModified = fileInfo.CreationTime.ToString("yyyy-MM-dd");
                else
                    _recreatePageSitemap = true;
            }
            else
                _recreatePageSitemap = true;

            if (_recreatePageSitemap)
            {
                var pages = web.WebPages.Where(p => p.AllowAnonymous && !(p.IsStatic && p.IsShared && !string.IsNullOrEmpty(p.ViewData))).OrderByDescending(p => p.LastModified);

                int _curWebID = context.RootWeb.Id;

                var _pageUrlset = new SitemapUrlset() { Urls = new List<SitemapUrl>() };
                foreach (var page in pages)
                {

                    if (page.IgnoreRouteKeys != null && page.IsShared)
                        continue;

                    _pageUrlset.Urls.Add(new SitemapUrl()
                    {
                        Url = string.IsNullOrEmpty(page.LinkUrl) ? UrlResolver.Resolve(context.HttpContext, page) : (UrlResolver.Resolve(context.RootWeb)+UrlUtility.CreateUrlHelper().Content(page.LinkUrl)),
                        LastModified = page.LastModified.ToString("yyyy-MM-dd"),
                        Priority = 0.5M,
                        ChangeFrequently = SitemapChangeFrequently.Weekly
                    });
                }
                _pageSitemap.LastModified = DateTime.Now.ToString("yyyy-MM-dd");
                DNA.Utility.XmlSerializerUtility.SerializeToXmlFile(sitemapPath + web.Name + ".xml", typeof(SitemapUrlset), _pageUrlset);
            }
            return _pageSitemap;
        }
    }
}